<?php
/**
 * 支付模型
 * @author cjli
 *
 */
class PaymentModel extends Model
{
	
	/**
	 * 支付方式列表
	 */
	public static function get_payment_list()
	{
		global $language_id;
		$cacheId = 'payment_list_'.$language_id;
		if(S($cacheId)){
			$paymentList = S($cacheId);
		}else{
	    $payment_db = D('payment');
	    $where = array(
	    	'language_id' => $language_id,
	    	'status' => 1,
	    );
	    $paymentList = array();
	    $payment_db->join('payment_items ON payment_items.payment_name = payment.name');
	    $result = $payment_db->where($where)->order('listorder DESC')->select();
	    if(empty($result)){
	    	$where = array(
		    	'language_id' => C('language_id'),
		    	'status' => 1,
		    );
		    $paymentList = array();
		    $payment_db->join('payment_items ON payment_items.payment_name = payment.name');
		    $result = $payment_db->where($where)->order('listorder DESC')->select();
	    }
	    if($result){
	    	foreach ($result as $payment){
	    		if(empty($payment['title'])){
	    			$payment['title'] = D('payment_items')->where(array('name' => $payment['name'], 'language_id' => C('language_id')))->getField('title');
	    		}
	    		$payment['config'] = unserialize($payment['config']);
	    		$paymentList[$payment['name']] = $payment;
	     	}
	    }
	    S($cacheId, $paymentList);
		}
    return $paymentList;
	}
	
	/**
   * 获取支付方式信息
   * @param string $payment 支付方式标识
   */
	public static function get_payment_info($paymentname)
	{
		$paymentname = ucfirst(strtolower($paymentname));
		$paymentList = self::get_payment_list();
		return isset($paymentList[$paymentname]) ? $paymentList[$paymentname] : false;
	}
	
	/**
	 * 获取支付手续费
	 * @param string $paymentname 支付方式名称
	 * @param float $amount 支付总费用
	 * @param int $partner 是否合作伙伴	 1 是 2否 
	 * @param float $weight 重量	 
	 */
	public static function get_paymeent_fee($paymentname, $amount,$partner = 0, $weight = 0)
	{
		$fee = 0;
		$amount = floatval($amount);
		if($amount == 0){
			return $fee;
		}
		$paymentInfo = self::get_payment_info($paymentname);
		if($paymentInfo && $paymentInfo['status'])
		{
			//按百分比收费
			if($paymentInfo['poundage_type'] == 1)
			{
				$fee = $paymentInfo['poundage'] * $amount / 100;
			}
			//按固定额度收费
			elseif($paymentInfo['poundage_type'] == 2)
			{
				$fee = $paymentInfo['poundage'];
			}
			$fee = number_format($fee, 2, '.', '');
		}

	    /**
	    *超重附加费  /  预约量体费用   上门量体 商品总金额低于2999 元的另外收取99 元的量体费用
	    *在结算页面，若有身形人超过265lbs(120kg)的定制商品，
	    *总价前加入超重费（Plus-size Fee），为超重身形定制商品价格的50%
	    **/
	    $amount_overweight = 0;
		$cartList = $partner ? CartModel::get_cart_product_list_partner() : CartModel::get_cart_product_list();
		foreach ($cartList['productList'] as &$cart){
			if($cart['pid']){
                $attr_arr=unserialize($cart['attr']);
                foreach ($attr_arr as $k=>$v){
                    //定制选项附加费用
                    $attr_value_id= explode(":",$attr_arr[$k]);
                    $attr_value_id=$attr_value_id[count($attr_value_id)-1];
                    $add_money=AttributeModel::get_attribute_values_info($attr_value_id);
                    if($add_money['add_money']){
                        $fee +=$add_money['add_money']*$cart['qty'];
                    }
                }
                $product = ProductModel::get_product_info($cart['pid']);
				$product['price'] = ProductModel::get_final_price($cart['pid'], $cart['qty'], true, $attr, $cart['custom_type']);
				if($attribute_type){
					$product['goods_amount']  = $product['sell_price'] * $cart['qty'];
					$product['price'] = $product['sell_price'];
				}else{
					$product['goods_amount']  = $product['price'] * $cart['qty'];
				} 
				$product['original_price'] = $product['goods_amount'];  //原价
			    $product['goods_overweight'] = 0;

                $overweight = $weight;
		    	if( intval($cart['measure_id']) > 0 ){
			    	$measure = MeasureModel::get_measure_one(array('id'=>$cart['measure_id']));
		    	    $overweight = floatval($measure['weight']);
		    	}
                if($partner==0){
                    if( floatval($overweight) >= 120){
                        $product['goods_overweight'] = $cart['price'] * 0.5 * $cart['qty'];

                        if($cart['pants_qty']>0){
                            $product['pants_price']=$product['pants_price']*$cart['pants_qty']*0.5;
                            $product['goods_overweight']=$product['goods_overweight']+$product['pants_price'];
                        }
                    }
                }else{
                    if( floatval($overweight) >= 120){
                        if($cart['product']["model_id"]!=16&&$cart['product']["model_id"]!=25){//鞋子配置不算超重费
                            $product['goods_overweight'] = $cart['price'] * 0.5 * $cart['qty'];
                            if($cart['pants_qty']>0){
                                $product['pants_price']=$product['pants_price']*$cart['pants_qty']*0.5;
                                $product['goods_overweight']=$product['goods_overweight']+$product['pants_price'];
                            }
                        }
                    }
                }
				$amount_overweight  += $product['goods_overweight']; //超重附加费
			}
            $last_measyre_id=$cart['measure_id'];
		}
		$fee += $amount_overweight;
        $measure_info=MeasureModel::get_measure_one(array('id'=>$last_measyre_id));
        if($measure_info['m_status']==10){
           $class_id=D("order_measure")->where(array('id'=>$measure_info['order_measure_id']))->getField("class_id");
        }
        if($class_id==2&&$cartList['total_amount']<2999){
            $fee +=99;
        }
		return $fee;
		
	}
}